package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"
	"goods/models/mysql"
	"strings"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *goodsrpc.CommentAddRequest) (*goodsrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods
	mysql.DB.Debug().Where("id=?", in.GoodsId).Find(&goods)
	if goods.ID == 0 {
		return nil, fmt.Errorf("找不到该商品")
	}

	comment := mysql.Comment{
		Context: in.Context,
		GoodsId: in.GoodsId,
		UserId:  in.UserId,
	}
	if strings.Contains(in.Context, "欺诈") || strings.Contains(in.Context, "伪劣") {
		return nil, fmt.Errorf("包含敏感词汇发送失败")
	}

	mysql.DB.Debug().Create(&comment)
	return &goodsrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
